package com.dm.shop.utils;

import com.dm.shop.entity.Admin;
import com.dm.shop.entity.User;
import com.dm.shop.service.AdminService;
import com.dm.shop.service.UserService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
//import org.springframework.security.core.Authentication;
/**
 * @version 1.0
 * @author: Saiya Wang
 * @desc:全局公共工具类
 * @date: 2021/7/21 10:19
 */
@Slf4j     //使用log打印日志
@Component //组件注解 把普通pojo实例化到spring容器
public class CommonUtil {
    private static UserService userService;

    private static AdminService adminService;

    @Autowired
    public void setUserService(UserService userService)
    {
        CommonUtil.userService = userService;
    }

    @Autowired
    public void setAdminService(AdminService adminService)
    {
        CommonUtil.adminService = adminService;
    }

    /**
     * 获取当前登录的用户信息
     * @return
     */
    public static User getCurrentUser()
    {
        /**
         SecurityContextHolder.getContext()获取安全上下文对象，就是那个保存在 ThreadLocal 里面的安全上下文对象
         总是不为null(如果不存在，则创建一个authentication属性为null的empty安全上下文对象)
         获取当前认证了的 principal(当事人),或者 request token (令牌)
         如果没有认证，会是 null,该例子是认证之后的情况
         */
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //判断有没有登录的用户，有就返回登录用户，没有就返回null
        if(authentication != null)
        {
            //没有
            if(authentication instanceof AnonymousAuthenticationToken)
            {
                return null;
            }
            //有用户登录
            if(authentication instanceof Authentication)
            {
                Integer uid = Integer.valueOf(authentication.getName());
                log.info("当前用户的ID：{}"+uid);
                User user = userService.getById(uid);
                log.info("当前认证用户："+user);
                return user;
            }
        }
        log.info("当前用户没有登录");
        return null;
    }

    /**
     * 获取当前登录的管理员
     * @return
     */
    public static Admin getCurrentAdmin()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null)
        {
            //没有管理员登录
            if(authentication instanceof AnonymousAuthenticationToken)
            {
                return null;
            }
            if(authentication instanceof Authentication)
            {
                Integer uid = Integer.valueOf(authentication.getName());
                log.info("当前管理员ID:{}"+uid);
                Admin admin = adminService.getById(uid);
                return admin;
            }
        }
        log.info("当前没有管理员登录");
        return null;
    }
}
